﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Serialization;
using FontDialogSample;
using Microsoft.Win32;

namespace UniversalGrid
{
    /// <summary>
    /// Interaction logic for ConfigWindow.xaml
    /// </summary>
    public partial class ConfigWindow : Window
    {
        public ConfigWindow()
        {
            DataContext = App.ApplicationViewModel;
            InitializeComponent();
            lbGrids.SelectedItem = (DataContext as GridListViewModel).GridList.FirstOrDefault();
        }

        private void ConfigWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FontChooser chooser = new FontChooser();
            if (chooser.ShowDialog() == true)
            {
                ((sender as Button).DataContext as GridViewModel).FontFamily = chooser.SelectedFontFamily;
                ((sender as Button).DataContext as GridViewModel).FontStretch = chooser.SelectedFontStretch;
                ((sender as Button).DataContext as GridViewModel).FontSize = chooser.SelectedFontSize;
                ((sender as Button).DataContext as GridViewModel).FontStyle = chooser.SelectedFontStyle;
                ((sender as Button).DataContext as GridViewModel).FontWeight = chooser.SelectedFontWeight;
            } 
        }

        private void OpenMenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            dialog.Multiselect = false;
            dialog.Filter = "Universal Grid Templates (*.ugt)|*.ugt|All files (*.*)|*.*";
            dialog.DefaultExt = "ugt";
            var result = dialog.ShowDialog();
            if (result == true)
            {
                XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<GridViewModel>));

                using (StreamReader reader = File.OpenText(dialog.FileName))
                {
                    App.ApplicationViewModel.GridList.Clear();
                    lbGrids.UpdateLayout();
                    foreach ( var gridViewModel in (ObservableCollection<GridViewModel>)xs.Deserialize(reader))
                    {
                        App.ApplicationViewModel.GridList.Add(gridViewModel);        
                    }
                    if (lbGrids.Items.Count > 0)
                    {
                        lbGrids.SelectedItem = lbGrids.Items[0];
                    }
                }    
            }
        }

        private void SaveMenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            dialog.AddExtension = true;
            dialog.OverwritePrompt = true;
            dialog.DefaultExt = "ugt";
            dialog.Filter = "Universal Grid Templates (*.ugt)|*.ugt|All files (*.*)|*.*";
            var result = dialog.ShowDialog();
            if (result == true)
            {
                XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<GridViewModel>));

                using (StreamWriter writer = File.CreateText(dialog.FileName))
                {
                    xs.Serialize(writer,App.ApplicationViewModel.GridList);
                }   
            }
        }

        private void QuitMenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }
    }
}
